package ru.nsu.effel.actions;

import java.util.Iterator;
import java.util.List;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationErrors;
import ru.nsu.effel.persistence.AccountManager;
import ru.nsu.effel.persistence.ConnectionBid;
import ru.nsu.effel.persistence.ConnectionBidManager;

@UrlBinding("/ConnectionBidController.action")
public class ConnectionBidControllerActionBean implements ActionBean {
	private ActionBeanContext context;
    @Validate(required=true, minlength=3)
	private String username;
    @Validate(required=true, minlength=3)
	private String password;
    @Validate(required=true)
	private String firstname;
    @Validate(required=true)
	private String lastname;
    @Validate(required=true)
	private String address;
	private String comments;
	@Validate(required=true)
	private int tariff_id;
	
	public ActionBeanContext getContext() {
		return context;
	}

	public void setContext(ActionBeanContext context) {
		this.context = context;
	}
	
	@DefaultHandler
	@HandlesEvent("createBid")
	public Resolution createBid() {
		ValidationErrors verrors = getContext().getValidationErrors();
		SimpleError eerror = new SimpleError("Username is already exists");
		
		List credentials = AccountManager.getInstance().getCredentials();
		for(Iterator iter = credentials.iterator(); iter.hasNext(); ) {
			Object[] response = (Object[])iter.next();
			if ((username.equals((String)response[1]))) {
				verrors.add("username", eerror);
				getContext().setValidationErrors(verrors);
				return getContext().getSourcePageResolution();
			}
		}
		
		ConnectionBidManager cbmanager = ConnectionBidManager.getInstance();
		
		List<ConnectionBid> bids = cbmanager.getAllConnectionBids();
		for (Iterator<ConnectionBid> iter = bids.iterator(); iter.hasNext();) {
			ConnectionBid bid = iter.next();
			if (bid.getUsername().equals(username)) {
				verrors.add("username", eerror);
				getContext().setValidationErrors(verrors);
				return getContext().getSourcePageResolution();				
			}
		}
		
		cbmanager.addConnectionBid(new ConnectionBid(0, username, password, tariff_id, firstname, lastname, address, comments, 0));
		
		return new RedirectResolution("/index.jsp");
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public int getTariff_id() {
		return tariff_id;
	}

	public void setTariff_id(int tariffId) {
		tariff_id = tariffId;
	}
}
